<?php 
$sys_pages = unserialize(SYS_PAGES);
$search = Utils::get_param('search', '');
$exit = Utils::get_param('__exit', 0);
if ($exit == 1) {
  unset($_SESSION['user']);
  setcookie('user', '', time() - 3600);
  $url = str_replace('&__exit=1', '', Utils::get_url());
  $url = str_replace('?__exit=1', '', $url);
  header('location: ' . $url);
  return;
}

if (!isset($_SESSION['profile'])) {
  $rslt = $orm->get('pb_ecom_admin', NULL, NULL, NULL, NULL, NULL)->d;
  if (count($rslt) > 0) {
    $_SESSION['profile'] = $rslt[0];
  }
}

$user = null;
if (isset($_SESSION['user'])) {
  $user = json_decode($_SESSION['user']);
}

$cart = array();
if (isset($_SESSION['cart'])) {
  $cart = json_decode($_SESSION['cart']);
}
?>

<!DOCTYPE html>
<html>
  <head>
    <title><?php echo ((isset($_SESSION['profile'])) ? $_SESSION['profile']->shop_name : ''); ?></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0" />
    <?php include '../include/common.inc.php'; ?>
    <style type="text/css"><?php echo $app_data->contents[$app_data->query]->css; ?></style>
    
    <?php 
      if (!isset($app_data->contents[$app_data->query]->__iframe) || $app_data->contents[$app_data->query]->__iframe == 0) {
        $hdfile = str_replace('.php', '', $app_data->contents[$app_data->query]->html) . '.hd.php';
        if (file_exists($hdfile)) {
          include $hdfile;
        }
      }
    ?>
    
    <link rel="stylesheet" href="<?php echo TH_DIR . basename(dirname(__FILE__)); ?>/megamenu/sky-mega-menu.css" />
    
    <!--[if lt IE 9]>
      <link rel="stylesheet" href="<?php echo TH_DIR . basename(dirname(__FILE__)); ?>/megamenu/sky-mega-menu-ie8.css">
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    
    <!--[if lt IE 10]>
      <script type="text/javascript" src="<?php echo TH_DIR . basename(dirname(__FILE__)); ?>/megamenu/jquery.placeholder.min.js"></script>
    <![endif]-->
  </head>

  <body class="pure-skin-mine">

    <div class="cs_toolbar_wrapper">
      <div class="cs_toolbar">
        <img style="height: 55px;" src="<?php echo TH_DIR . basename(dirname(__FILE__)); ?>/skin/images/logo.png" alt="logo"></img>
        <a class="cs_btn_cart" style="margin-right: 10px;" href="#" onclick="return Url.redirect(____redmd, ____sys + '.php?i=' + <?php echo ECOM_ID; ?> + '&__file=index_cart');">购物车 (<span id="cs_cart">&nbsp;</span>)</a>
        <a class="cs_btn_li" id="cs_login" <?php if (isset($_SESSION['user'])) { ?>style="display: none;"<?php } ?> onclick="return Url.redirect(____redmd, ____sys + '.php?i=' + ____login_id + '&u=' + Utils.encodeURI(document.URL));" href="#">登录</a>
        <a class="cs_btn_reg" id="cs_register" <?php if (isset($_SESSION['user'])) { ?>style="display: none;"<?php } ?> onclick="return Url.redirect(____redmd, ____sys + '.php?i=' + ____login_id + '&u=' + Utils.encodeURI(document.URL));" href="#">注册</a>
        <a class="cs_btn_acct" id="cs_account" <?php if (!isset($_SESSION['user'])) { ?>style="display: none;"<?php } ?> href="#" onclick="return Url.redirect(____redmd, ____sys + '.php?i=' + <?php echo ECOM_ACCT_ID; ?>);">我的账号</a>
        <a class="cs_btn_lo" id="cs_logout" <?php if (!isset($_SESSION['user'])) { ?>style="display: none;"<?php } ?> onclick="Utils.block_ui(); location.href=document.URL.replace('#', '') + ((document.URL.indexOf('?') == -1) ? '?__exit=1' : '&__exit=1'); return false;" href="#">注销</a>
      </div>
    </div>
    <div>&nbsp;</div>
    
    
    <div class="cs_header">
      <div class="cs_menubar"></div>
    </div>
    <div class="cs_wrapper">
      <div class="cs_navbar ui-widget-header ui-corner-all" style="display: none;"></div>
      <div class="cs_body">
        <?php 
          if (isset($app_data->contents[$app_data->query]->__iframe) && $app_data->contents[$app_data->query]->__iframe == 1) {
            $sys_pages = unserialize(SYS_PAGES);
            if (in_array($app_data->param_id, $sys_pages)) {
              echo $app_data->contents[$app_data->query]->html;
            } else {
              echo '<div class="cs_custom">' . $app_data->contents[$app_data->query]->html . '</div>';
            }
            
          } else {
            if (file_exists($app_data->contents[$app_data->query]->html)) {
              include $app_data->contents[$app_data->query]->html;
            }
          }
        ?>
      </div>
    </div>
      
        
        
    <div class="cs_f_up">&nbsp;</div>
    <div class="cs_f_bg">
      <div>&nbsp;</div>
      <?php include 'inc.foot.php'; ?>
      <div>&nbsp;</div>
      <?php echo $_SESSION['profile']->footer; ?>
    </div>
    
    <script type="text/javascript">
      /*<![CDATA[*/
      var ____cart = JSON.parse('<?php echo addslashes(json_encode($cart)); ?>');
      var ____user = JSON.parse('<?php echo addslashes(json_encode($user)); ?>');
      var ____sys_pages = JSON.parse('<?php echo addslashes(json_encode($sys_pages)); ?>');
      var ____search = '<?php echo addslashes($search); ?>';
      
      $(function(){
        Utils.set_cartnum(____cart);
        ____toolbar();
      });
      
      function ____func(parsed_url, rslt, elm) {
        var id = parsed_url.queryKey.i ? parsed_url.queryKey.i : ____default_id;
        var file = parsed_url.queryKey.__file ? parsed_url.queryKey.__file : 'index';
        if (Utils.exist(id, ____sys_pages)) {
          elm.html(rslt);
        } else {
          elm.html('<div class="cs_custom">' + rslt + '</div>');
        }
        $(document).tooltip();
        $('.cs_hi').removeClass('cs_hi');
        ____mid = ____mmid(____root, id);
        $('.cs_menubar li[value="' + ____mid + '"]').addClass('cs_hi');
        ____search = Url.get_param('search', '');
        if (id != '<?php echo ECOM_CAT_ID; ?>') {
          ____search = '';
        }
        $('#glb_serach').val(____search);
        ____leftbar(id, file);
        ____nav(id, file);
        ____toolbar();
      };

      function ____toolbar() {
        if (!____user) {
          $('#cs_login').show();
          $('#cs_register').show();
          $('#cs_account').hide();
          $('#cs_logout').hide();
        } else {
          $('#cs_login').hide();
          $('#cs_register').hide();
          $('#cs_account').show();
          $('#cs_logout').show();
        }
      };

      function ____menubar(id, root) {
        var mmid = ____mmid(root, id);
        $(".cs_menubar").empty();
        $('.cs_menubar').append('<ul id="j_menu" class="sky-mega-menu sky-mega-menu-anim-scale sky-mega-menu-response-to-stack"></ul>');
        ____menu(root, $('#j_menu'), mmid);
        $("#j_menu").append(
          '<li class="right search">' +
            '<form action="#">' +
              '<div class="input">' +
                '<button type="submit" class="button" onclick="Url.redirect(____redmd, ____sys + \'.php?i=<?php echo ECOM_CAT_ID; ?>&search=\' + $(\'#glb_serach\').val());">Go</button>' +
                '<input id="glb_serach" type="text" placeholder="Search" value="' + ____search + '">' +
              '</div>' +
            '</form>' +
          '</li>'
        );
        $('form').submit(false);
      };
      
      function ____menu(node, elm, mmid) {
        for (var idx in node.__c) {
          var itm = node.__c[idx];
          elm.append('<li' + ((mmid == itm.id) ? ' class="cs_hi"' : '') + ' value="' + itm.id +'">' +
                       '<a href="' + ____sys + '.php?i=' + itm.id + '" onclick="return Url.redirect(' + ____redmd + ', \'' + ____sys + '.php?i=' + itm.id + '\');">' + ((itm.icon == '') ? '' : '<i class="fa fa-fw fa-lg">' + itm.icon + '</i>') +itm.name + '</a>' +
                       ((itm.__c.length > 0) ? '<div class="grid-container3"><ul id="pgul_' + itm.id + '"></ul></div>' : '') + 
                     '</li>');
          if (itm.__c.length > 0) {
            ____menu(itm, $('#pgul_' + itm.id), mmid);
          }
        }
      };
      /*]]>*/
    </script>
    <?php include '../include/inc.theme.js.php'; ?>
  </body>
</html>